package com.blue.mapper.workflow;

import com.blue.po.workflow.TaskInstancePO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blue.vo.workflow.TaskInstanceVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 流程任务实例表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-03-07
 */
@Mapper
public interface TaskInstanceMapper extends BaseMapper<TaskInstancePO> {
    @Select("(SELECT t.id,t.operation_time,t.assignee_id,t.status,t.remark,t1.username as assignee,multi_type as multiType,t.create_time as createTime,\n" +
            "t.current_node_id as currentNodeId,t.sort,t.task_type as taskType,t.relate_task_id as relateTaskId,\n" +
            "(select GROUP_CONCAT(u.username SEPARATOR ',') from workflow_task_cc cc left join author_user u on cc.cc_to_id = u.user_id\n" +
            "where cc.instance_id = #{instanceId} and cc.task_instance_id= t.id) as ccTos\n" +
            "FROM workflow_task_instance t\n" +
            "LEFT JOIN author_user t1\n" +
            "ON t1.user_id = t.assignee_id\n" +
            "WHERE t.instance_id = #{instanceId})\n" +
            "UNION ALL\n" +
            "(SELECT t.id,t.start_time as operationTime, t.initiator_id as assigneeId ,'-1' as status,null as remark,t1.username as assignee,null as multiType,start_time as createTime,\n" +
            "null as currentNodeId,-1 as sort,null as taskType,null as relateTaskId,\n" +
            "(select GROUP_CONCAT(u.username SEPARATOR ',') from workflow_task_cc cc left join author_user u on cc.cc_to_id = u.user_id\n" +
            "where cc.instance_id = #{instanceId} and cc.task_instance_id is null) as ccTos\n" +
            "FROM workflow_instance t\n" +
            "LEFT JOIN author_user t1\n" +
            "ON t1.user_id = t.initiator_id WHERE t.id = #{instanceId})")
    List<TaskInstanceVO> taskNodeList(@Param("instanceId") String instanceId);
}
